home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c,comp.graphics.algorithms,rec.games.programmer
- Path: xylo.owl.de!aurel
- From: aurel@xylo.owl.de (Aurel Balmosan)
- Subject: Re: Speed question here...
- Followup-To: comp.lang.c,comp.graphics.algorithms,rec.games.programmer
- References: <4ftluh$1gkv@hearst.cac.psu.edu>
- Organization: Xylo-Systems
- Date: Wed, 21 Feb 1996 23:36:57 GMT
- X-Newsreader: TIN [UNIX 1.3 941216BETA PL0]
- Message-ID: <Dn5G9L.ss@xylo.owl.de>
-
- William Koscho (koscho@wjk130.rh.psu.edu) wrote:
- : I was curious as to how fast something like the
- : following would execute:
-
- : int x;
- : node *ptr; ptr in linked list
-
- : for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
- : for ( x = 0; x < max; x++ ) {
- : array[x] = array_other[x];
- : }
- : }
-
- : I really am not interested in the assignment statement, that's easy.
- : but the traversal through the linked list, and inner integer incremental
- : for loop for each node visited. How fast is a traversal through
- : a linked list when you do a for loop at each node?
-
- Your code uses about O(max * Number of list elements). You can reduce this
- to O(Number of list elements) by involving an object-based approach.
-
- In your code you copy an array to an other one. (I assume that this other
- array is a pointer within a list element). Because you copy always the
- same array to the other, why coping the array. Instate of copying the
- array you could reference this array with a pointer. So this array becomes
- a object of some kind. This object must be managed in the following ways:
- - creation
- - deletion
- - copying
- - duplicate if someone wants to change it and others are using
- it already and they want the old state.
-
- This is only a small overhead but it will avoid as much copy loop as
- possible.
-
- Other approaches to speed up your code by using different command orders
- are very subject to the used C-compiler and the way the compiler generates
- assembler code. (btw: If you want very good assembler code use the GCC)
-
-
- Aurel Balmosan
-
- --
-
- ----------------------------------------------------------------
- Aurel Balmosan aurel@xylo.owl.de
-